package com.xiaobaibai.controller;

import com.xiaobaibai.bo.EvaluationSelect;
import com.xiaobaibai.entity.ProductEvaluation;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IProductEvaluationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "评价管理相关接口")
@RequestMapping("/evaluation")
@CrossOrigin
public class ProductEvaluationController {

    @Autowired
    private IProductEvaluationService productEvaluationService;

    /**
     * 商品id,好评/中评/差评,回复,是否晒图   根据id倒序排列
     * 来一次真正的排序!sql优化也安排上
     */
    @PostMapping("/list")
    @ApiOperation("获取评价集合")
    public ResponseFormat getEvaluationList(@RequestBody EvaluationSelect evaluationSelect){
        evaluationSelect.setPageNum(evaluationSelect.getPageNum()-1);

        List<ProductEvaluation> list = productEvaluationService.getEvaluationList(evaluationSelect);
        int count = productEvaluationService.count();
        Map<String,Object> map=new HashMap<>();
        map.put("data",list);
        map.put("total",count);
        return ResponseFormat.ok("评价集合获取成功",map);
    }

    @DeleteMapping("/do-evaluation")
    @ApiOperation("删除评价")
    public ResponseFormat deleteEvaluation(@RequestParam(value = "id")Integer id){
        if(!productEvaluationService.removeById(id))
            return ResponseFormat.failure("删除失败");
        return ResponseFormat.ok("删除成功");
    }

    @PutMapping("/do-evaluation")
    @ApiOperation("回复评价")
    public ResponseFormat addEvaluationCustomer(@RequestBody Map<String,String> map){
        ProductEvaluation productEvaluation=new ProductEvaluation();
        productEvaluation.setId(Integer.parseInt(map.get("id")));
        productEvaluation.setCustomerContent(map.get("content"));
        if(!productEvaluationService.updateById(productEvaluation))
            return ResponseFormat.failure("回复失败");
        return ResponseFormat.ok("回复成功");
    }

}
